package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainPart;
import org.springblade.modules.admin.entity.MaintainReport;
import org.springblade.modules.admin.entity.MaintainReportSend;
import org.springblade.modules.admin.service.MaintainReportService;
import org.springblade.modules.system.entity.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Date 15:26 2024/7/17
 * @Description {
 *   报告
 * }
 * @Author lsay
 **/
@AllArgsConstructor
@RestController
@RequestMapping("/maintain/report")
public class MaintainReportController {

	private final MaintainReportService reportService;

	/**
	 * @Author lsay
	 * @Description {
	 *   根据维修工单数据生成报告
	 * }
	 * @Date 11:23 2024/7/18
	 * @Param 维修工单id
	 * @return 报告详情
	 **/
	@GetMapping("/select/by/wx/plan")
	public R<MaintainReport> selectReportByWxPlan(Long planId){
		return R.data(reportService.selectReportByWxPlan(planId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   新增或更新报告
	 * }
	 * @Date 11:24 2024/7/18
	 * @Param
	 * @return
	 **/
	@PostMapping("/submit")
	public R<?> submit(@RequestBody MaintainReport report){
		reportService.saveOrUpdate(report);
		return R.data(report.getId());
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   获取以生成数据的报告详情
	 * }
	 * @Date 11:24 2024/7/18
	 * @Param 报告id
	 * @return 报告详情
	 **/
	@GetMapping("/detail")
	public R<MaintainReport> detail(Long reportId){
		return R.data(reportService.detail(reportId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   查询报告分页
	 * }
	 * @Date 13:49 2024/7/22
	 * @Param
	 * @return
	 **/
	@GetMapping("/page")
	public R<IPage<MaintainReport>> page(MaintainReport report, Query query){
		return R.data(reportService.selectReportPage(Condition.getPage(query),report));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   查询报告列表
	 * }
	 * @Date 13:42 2024/7/23
	 * @Param
	 * @return
	 **/
	@GetMapping("/list")
	public R<List<MaintainReport>> list(MaintainReport report){
		return R.data(reportService.selectReportList(report));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   发送报告
	 * }
	 * @Date 8:54 2024/7/24
	 * @Param
	 * @return
	 **/
	@PostMapping("/send/{reportId}")
	public R<Boolean> sendReport(@PathVariable Long reportId){
		return R.data(reportService.sendReport(reportId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   报告发送记录
	 * }
	 * @Date 15:48 2024/7/18
	 * @Param
	 **/
	@PostMapping("/send/log")
	public R<Boolean> sendLog(@RequestBody MaintainReportSend send){
		return R.data(reportService.sendLog(send));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   查询报告发送记录
	 * }
	 * @Date 17:06 2024/7/22
	 * @Param
	 * @return
	 **/
	@GetMapping("/select/send/log")
	public R<List<MaintainReportSend>> selectReportSendLog(Long reportId){
		return R.data(reportService.selectReportSendLog(reportId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   报告列表导出
	 * }
	 * @Date 14:31 2024/7/30
	 * @Param
	 * @return
	 **/
	@PostMapping("/down/excel")
	public void downExcelDetail(HttpServletResponse response, @RequestBody MaintainReport report) throws IOException {
		List<List<String>> rows = new ArrayList<>();
		reportService.downExcel(report, rows);
		if(Func.isEmpty(rows)){
			throw new ServiceException("暂无报告数据导出");
		}
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<String> title = CollUtil.newArrayList
				("报告编号", "报告生成人", "人员类型", "报告发送状态", "报告生成时间", "任务编号",
					"项目名称", "电梯位置", "任务类型","任务完成时间");
			excelWriter = cn.hutool.poi.excel.ExcelUtil.getBigWriter(rows.size());
			excelWriter.setColumnWidth(0, 30);
			excelWriter.setColumnWidth(1, 30);
			excelWriter.setColumnWidth(2, 30);
			excelWriter.setColumnWidth(3, 30);
			excelWriter.setColumnWidth(4, 30);
			excelWriter.setColumnWidth(5, 30);
			excelWriter.setColumnWidth(6, 40);
			excelWriter.setColumnWidth(7, 40);
			excelWriter.setColumnWidth(8, 40);
			excelWriter.setColumnWidth(9, 40);

			// 单独设置头部
			excelWriter.writeHeadRow(title);
			String fileName = cn.hutool.core.date.DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			CommonUtil.setResponseHeader(response, fileName);
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}

}
